/**
 * 质控项目值域字典表管理初始化
 */
var SdsQceitemDic = {
    id: "SdsQceitemDicTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsQceitemDic.initColumn = function () {
    return [[
        {title: '代码', field: 'btCode', width : 200, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox',
                options:{required:true}
            }},
        {title: '描述', field: 'btDesc', width : 200, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox',
                options:{required:true}
            }},
        {title: '是否有效', field: 'isActive', width : 100, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                if(value==1){
                    return "<span class='label label-success'>是</span>";
                }else{
                    return "<span class='label label-warning'>否</span>";
                }
            },editor:{
                type:'combobox',
                options:{required:true,valueField:'id',textField:'text',editable:false,
                    data:
                        [
                            {'id':1,'text':'是'},
                            {'id':0,'text':'否'}
                        ]
                }
            }},
        {title: '更新标记', field: 'updateFlag', width : 100, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                if(value==1){
                    return "<span class='label label-success'>是</span>";
                }else{
                    return "<span class='label label-warning'>否</span>";
                }
            }}
        ]];
};

/**
 * 检查是否选中
 */
SdsQceitemDic.check = function () {
    var selected = $('#' + this.id).datagrid('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsQceitemDic.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加新一行表格
 */
SdsQceitemDic.addEItemDic = function () {
    var rowNum=$('#' + SdsQceitemDic.id).datagrid("getRows").length;
    //添加一行
    $('#' + SdsQceitemDic.id).datagrid('appendRow',{
        btCode: '',
        btDesc: '',
        isActive: 1,
        upDateFlag: 1,
        entityItemId:$("#entityItemId").val()
    });
    var rowIndex= rowNum;
    $('#' + SdsQceitemDic.id).datagrid('selectRow', rowIndex).datagrid('beginEdit', rowIndex);
};

/**
 * 保存数据到库表
 */
SdsQceitemDic.saveEItemDic = function () {
    //保存完毕后，同步数据
    var operation_save = function(){
        SdsQceitemDic.table.acceptChanges();//调用datagrid自带的方法，确认其修改 然后保存至数据库
        var rows =  $('#' + SdsQceitemDic.id).datagrid('getRows');
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            if (row != undefined && row != {}) {
                if (row.id != undefined) {
                    //update Row
                    Sys.ajax(Sys.backendPath + "/sds/sdsqceitemdic/update", row, function (data) {
                        Sys.success("修改成功!");
                        SdsQceitemDic.table.refresh();
                    },function(data){
                        Sys.error("修改失败!" + data.responseJSON.message + "!");
                    }, "json", false, "修改值域字典保存至库中..");
                } else {
                    //add Row
                    Sys.ajax(Sys.backendPath + "/sds/sdsqceitemdic/add", row, function (data) {
                        Sys.success("添加成功!");
                        SdsQceitemDic.table.refresh();
                    },function(data){
                        Sys.error("添加失败!" + data.responseJSON.message + "!");
                    }, "json", false, "新增值域字典保存至库中..");
                }
            }
        }
    };
    Sys.confirm("是否保存表格数据至数据库?",operation_save);
};

/**
 * 删除行同时作废数据库表数据
 */
SdsQceitemDic.delEItemDic = function () {
    if (SdsQceitemDic.check()) {
        var operation = function(){
            if(SdsQceitemDic.seItem.id!=null&&SdsQceitemDic.seItem.id!=""){
                var ajax = new $ax(Sys.backendPath + "/sds/sdsqceitemdic/delete", function (data) {
                    Sys.success("作废成功!");
                    SdsQceitemDic.table.refresh();
                }, function (data) {
                    Sys.error("作废失败!" + data.responseJSON.message + "!");
                });
                ajax.set("sdsQceitemDicId",SdsQceitemDic.seItem.id);
                ajax.start();
            }else{
                SdsQceitemDic.table.delRow();
            }
        };
        Sys.confirm("是否删除该行数据?",operation);
    }
};

/**
 * 查询质控项目值域字典表列表
 */
SdsQceitemDic.search = function () {
    var queryData = {};
    queryData['btCode'] = $("#btCode").val();
    queryData['btDesc'] = $("#btDesc").val();
    SdsQceitemDic.table.refresh({queryParams: queryData});
};

$(function () {
    var entityItemId = $("#entityItemId").val()
    var defaultColunms = SdsQceitemDic.initColumn();
    var table = new HISUITable(SdsQceitemDic.id, Sys.backendPath + "/sds/sdsqceitemdic/list/"+entityItemId, defaultColunms);
    table.setPaginationType("server");
    table.setRowNumbers(true);
    SdsQceitemDic.table = table.init();
});
